package com.css.dzj.dzjdzqs.modules.updown.controller;



import com.css.dzj.dzjdzqs.common.util.R;
import com.css.dzj.dzjdzqs.modules.updown.entity.CommAttachmentEntity;
import com.css.dzj.dzjdzqs.modules.updown.service.AttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/ud")
public class UpdAndDownController {
    @Autowired
    private AttachService attachService;

    //项目模块下所有附件目录
    @RequestMapping("/selectId")
    public R selectAllId(String id) {
        List<CommAttachmentEntity> attachmentEntities = attachService.queryAll(id);
        if (attachmentEntities != null && attachmentEntities.size() > 0) {
            return R.ok().put("data", attachmentEntities);
        }
        return R.error("附件未上传");
    }

    @RequestMapping("/save")
    public R save(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "id") String id) {
        try {
            String s = attachService.save(file, id);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("上传错误");
        }
        return R.ok().put("data", "ok");
    }

    @RequestMapping("/download")
    public String download(String id) {
        try {
            attachService.download(id);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("下载出现错误").toString();
        }
        return R.ok().put("data", "ok").toString();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public R del(String id) {
        String resultInfo = attachService.del(id);
        return R.ok().put("data", resultInfo);
    }
}
